
Partial Class Admin_HomePageDashboard
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
    End Sub

    Private Sub BindGridView()
        Try
            If String.IsNullOrEmpty(Me.RolesDropDownList.SelectedValue) Then
                Exit Sub
            End If
            Dim selectedRoleId As Integer = CInt(Me.RolesDropDownList.SelectedValue)
            Dim DashboardList As System.Collections.Generic.List(Of Cdm.DataCore.Navigation.Dashboard) = Cdm.DataCore.Navigation.Dashboard.ListByRole(selectedRoleId)
            For i As Integer = DashboardList.Count + 1 To 6
                Dim emptyDashboard As Cdm.DataCore.Navigation.Dashboard = New Cdm.DataCore.Navigation.Dashboard(selectedRoleId, i, "", "")
                DashboardList.Add(emptyDashboard)
            Next
            Me.DashboardGridView.DataSource = DashboardList
            Me.DashboardGridView.DataBind()
        Catch ex As Exception
            Dim message As String = ex.Message
            Throw ex
        End Try
    End Sub

    Protected Sub SubmitRoleButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitRoleButton.Click
        BindGridView()
    End Sub

    Protected Sub DashboardGridView_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles DashboardGridView.RowCreated
        If e.Row.DataItem IsNot Nothing Then
            Dim dashboardItem As Cdm.DataCore.Navigation.Dashboard
            dashboardItem = CType(e.Row.DataItem, Cdm.DataCore.Navigation.Dashboard)
            Dim dashboardDropDown As DropDownList = e.Row.FindControl("DashboardItemDropDownList")
            dashboardDropDown.SelectedValue = dashboardItem.ControlName
        End If
    End Sub

    Protected Sub RolesDropDownList_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles RolesDropDownList.DataBound
        If Not Me.IsPostBack Then
            BindGridView()
        End If
    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CancelButton.Click
        BindGridView()
        SetStatus("Settings have been reset.")
    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        Dim trans As New Gentle.Framework.Transaction()
        Try
            Dim currentDisplayOrder As Integer = 1
            Dim currentRole As Integer = CInt(Me.RolesDropDownList.SelectedValue)
            DeleteCurrentSettings(trans)
            For i As Integer = 0 To Me.DashboardGridView.Rows.Count - 1
                Dim dashboardDropDown As DropDownList = DashboardGridView.Rows(i).FindControl("DashboardItemDropDownList")
                If dashboardDropDown IsNot Nothing Then
                    If Not String.IsNullOrEmpty(dashboardDropDown.SelectedValue) Then
                        Dim titleText As TextBox = DashboardGridView.Rows(i).FindControl("TitleTextBox")
                        Dim newDashboard As New Cdm.DataCore.Navigation.Dashboard()
                        newDashboard.Title = titleText.Text
                        newDashboard.ControlName = dashboardDropDown.SelectedValue
                        newDashboard.RoleId = currentRole
                        newDashboard.DisplayOrder = currentDisplayOrder
                        newDashboard.Persist(trans)
                        currentDisplayOrder += 1
                    End If
                End If
            Next

            'Commit the transaction and reset the Grid View
            trans.Commit()
            BindGridView()
            SetStatus("Home Page Items Saved.")

        Catch ex As Exception
            trans.Rollback()
            Throw ex
        End Try
    End Sub
    Private Sub ClearStatus()
        SetStatus("")

    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub

    ''' <summary>
    ''' Delete the current dashboard settings for the current role.
    ''' Keep in a Gentle Transaction so things may be rolled back if something fails.
    ''' </summary>
    ''' <param name="trans"></param>
    ''' <remarks></remarks>
    Private Sub DeleteCurrentSettings(ByVal trans As Gentle.Framework.Transaction)
        Dim DashboardDeleteList As System.Collections.Generic.List(Of Cdm.DataCore.Navigation.Dashboard)
        DashboardDeleteList = Cdm.DataCore.Navigation.Dashboard.ListByRole(CInt(Me.RolesDropDownList.SelectedValue))
        If DashboardDeleteList Is Nothing Then Exit Sub
        For i As Integer = DashboardDeleteList.Count - 1 To 0 Step -1
            DashboardDeleteList(i).Remove(trans)
        Next
    End Sub
End Class
